package cn.toutatis.sandbox.root.systemService.controller;


import cn.toutatis.common.config.PagingQuery;
import cn.toutatis.common.controllerDepand.BaseControllerImpl;
import cn.toutatis.common.controllerDepand.Polymerization;
import cn.toutatis.common.log.LogBackHandle;
import cn.toutatis.common.result.Actions;
import cn.toutatis.common.result.Result;
import cn.toutatis.sandbox.root.systemService.entity.SystemDictionary;
import cn.toutatis.sandbox.root.systemService.service.SystemDictionaryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 静态字典 前端控制器
 * </p>
 *
 * @author Toutatis_Gc
 * @since 2020-07-09
 */
@Polymerization
@LogBackHandle("SystemDictionaryController 静态字典")
@RequestMapping("/root/systemDictionary")
public class SystemDictionaryController extends BaseControllerImpl<SystemDictionary, SystemDictionaryService> {

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("root/system/setting/SystemDictionaryIndex");
        modelAndView.addObject("status",service.staticDic("SYSTEM_STATUS_0000"));
        modelAndView.addObject("classify",service.staticDic("SYS_CLASSIFY_0000"));
        return modelAndView;
    }

//    @Ignore(ignore = {"cn.tou","bb.aa"})
    @RequestMapping(value = "/getTree",method = RequestMethod.POST)
    public Result getTree(PagingQuery pagingQuery, SystemDictionary systemDictionary){
        List<SystemDictionary> list = service.getTree(systemDictionary.getParentId(),pagingQuery);
        Map<String,Object> map = new HashMap<>(2);
        map.put("tree",list);
        int count = service.count();
        map.put("total",count);
        result.setContent(map,Actions.SELECT);
        return result;
    }

    @RequestMapping(value = "/getSelectList",method = RequestMethod.POST)
    public Result getSelectList() {
        QueryWrapper<SystemDictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        List<SystemDictionary> list = service.list(queryWrapper);
        result.setContent(list,Actions.SELECT);
        return result;
    }

    @RequestMapping(value = "/getClassifyTable",method = RequestMethod.POST)
    public Result getClassifyTable(){
        QueryWrapper<SystemDictionary> selectOpenWrapper = commonWrapper.selectOpenWrapper();
        selectOpenWrapper.eq("code","SYS_CLASSIFY_0000");
        SystemDictionary father = service.getOne(selectOpenWrapper);
        QueryWrapper<SystemDictionary> singletonEqWrapper = commonWrapper.singletonEqWrapper("parentId", String.valueOf(father.getId()));
        List<SystemDictionary> list = service.list(singletonEqWrapper);
        result.setContent(list, Actions.SELECT);
        return result;
    }
}
